<?php


namespace app\store\controller\apps\lottery;


use app\store\controller\Controller;
use app\store\model\lottery\Action as ActionModel;
use app\store\model\lottery\Goods as GoodsModel;
use app\store\model\User as UserModel;

class Action extends Controller
{

    /**
     * 抽奖活动列表
     */
    public function index()
    {
        $list = ActionModel::getList();
        return $this->fetch('index', compact(['list']));
    }

    /**
     * 新增
     */
    public function add(ActionModel $actionModel)
    {
        if (!$this->request->isAjax()) {
            $users = UserModel::all();
            return $this->fetch('add');
        }
        if ($actionModel->add($this->addParam())) {
            return $this->renderSuccess('添加成功', url('apps.lottery.action/index'));
        }
        return $this->renderError($actionModel->getError() ?: '添加失败');
    }

    /**
     * 上下线
     */
    public function state($id)
    {
        $lottery = ActionModel::find($id);
        if (!$lottery) {
            return $this->renderError('奖品不存在');
        }
        if ($lottery->online) {
            $lottery->online = 0;
        } else {
            $lottery->online = 1;
        }
        $result = $lottery->save();
        return $result ? $this->renderSuccess('请求成功') : $this->renderError('请求失败');
    }

    /**
     * 删除
     */
    public function delete($id)
    {
        $lottery = ActionModel::find($id);
        if ($lottery->delete()) {
            return $this->renderSuccess('删除成功', url('apps.lottery.action/index', ['id' => $id]));
        }
        return $this->renderError($lottery->getError() ?: '删除失败');
    }

    private function addParam()
    {
        $data = $this->request->post('action/a');
        $integral = $this->request->post('integral');
        $day_way = $this->request->post('day_way');
        $count_way = $this->request->post('count_way');
        $users = $this->request->post('users');
        if ($data['way'] == 1) {
            //每天N次
            $data['count'] = $day_way;
        } else {
            //每人N次
            $data['count'] = $count_way;
        }
        if ($data['integral']) {
            $data['integral'] = $integral;
        }
        if ($users) {
            $data['users'] = json_encode($data['users']);
        } else {
            $data['users'] = $users;
        }
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        return $data;
    }

    /**
     * 奖品
     */
    public function goods($id)
    {
        $list = GoodsModel::getList($id);
        return $this->fetch('goods', compact(['list', 'id']));
    }

    /**
     * 添加奖品
     */
    public function addGoods($lottery_id, GoodsModel $goodsModel)
    {
        if (!$this->request->isAjax()) {
            return $this->fetch('addGoods');
        }
        $data = $this->addGoodsParam();
        $data['lottery_id'] = $lottery_id;
        if ($goodsModel->add($data)) {
            return $this->renderSuccess('添加成功', url('apps.lottery.action/goods', ['id' => $lottery_id]));
        }
        return $this->renderError($goodsModel->getError() ?: '添加失败');
    }

    private function addGoodsParam()
    {
        $data = $this->request->post('goods/a');
        switch ($data['type']) {
            case 2:
                $data['prize'] = $data['integral'];
                break;
            case 3:
                $data['prize'] = $data['coupon_id'];
                break;
            case 4:
                $data['prize'] = $data['goods_id'];
                break;
        }
        return $data;
    }

    /**
     * 编辑奖品
     */
    public function editGoods($id)
    {
        $good = GoodsModel::detail($id);
        if (!$this->request->isAjax()) {
            return $this->fetch('editGoods', compact(['good']));
        }
        if ($good->edit($this->addGoodsParam())) {
            return $this->renderSuccess('修改成功', url('apps.lottery.action/goods', ['id' => $good['lottery_id']]));
        }
        return $this->renderError($good->getError() ?: '添加失败');
    }

    /**
     * 删除奖品
     */
    public function deleteGoods($id)
    {
        $good = GoodsModel::find($id);
        if ($good->delete()) {
            return $this->renderSuccess('删除成功', url('apps.lottery.action/goods', ['id' => $good['lottery_id']]));
        }
        return $this->renderError($good->getError() ?: '删除失败');
    }

}